package com.warm.androidstudy.utils;

import android.util.Base64;

import java.lang.reflect.Method;

/**
 * 针对Base64的加密和解密
 * Created by Administrator on 2017/11/6.
 */

public class Base64Util {
    /***
     * encode by Base64
     */
    public static String encodeBase64(byte[] input) throws Exception {
        Class clazz = Class.forName("com.sun.org.apache.xerces.internal.impl.dv.util.Base64");
        Method mainMethod = clazz.getMethod("encode", byte[].class);
        mainMethod.setAccessible(true);
        Object retObj = mainMethod.invoke(null, new Object[]{input});
        return (String) retObj;
    }

    //    byte[] key= Base64.decode("YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4".getBytes(), Base64.DEFAULT);
    public static String decodeStr(String str) {
        byte[] bt = null;
        bt = Base64.decode(str.getBytes(), Base64.DEFAULT);
        String s = new String(bt);
        return s;
    }
}
